//
//  HNSH_GroupNameViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/18.
//

#import "HNSH_GroupNameViewController.h"

@interface HNSH_GroupNameViewController ()
/** */
@property (nonatomic, strong)UILabel * titleLabel;
/** */
@property (nonatomic, strong)UILabel * desLabel;
/** */
@property (nonatomic, strong)UIImageView * imgView;
/** */
@property (nonatomic, strong)UITextField * textField;
/** */
@property (nonatomic, strong)UIView * segView;
/** */
@property (nonatomic, strong)UIButton * doneBtn;
@end

@implementation HNSH_GroupNameViewController


//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    [self showNavTitle:@"" showLeftItem:YES];
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self addAllViews];
    [self addAllLayout];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.desLabel];
    [self.view addSubview:self.imgView];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.segView];
    [self.view addSubview:self.doneBtn];
}

- (void)addAllLayout{
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(kNavBarAndStatusBarHeight + 32);
        make.height.mas_equalTo(28);
    }];
    [self.desLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(22);
    }];
    [self.imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(32);
        make.width.height.mas_equalTo(40);
        make.top.mas_equalTo(self.desLabel.mas_bottom).offset(40);
    }];
    [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgView.mas_right).offset(12);
        make.right.mas_equalTo(-32);
        make.top.height.mas_equalTo(self.imgView);
    }];
    [self.segView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.textField);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.textField.mas_bottom);
    }];
    [self.doneBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(172);
        make.height.mas_equalTo(44);
        make.top.mas_equalTo(self.textField.mas_bottom).offset(90);
    }];
    
    [[self.textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *textField = x;
        if (textField.text.length > 15) {
            textField.text = [textField.text substringToIndex:15];
            NSLog(@"备注15字以内 ==> %@",textField.text);
        }
    }];
}

- (void)doneBtnAction{
    if (!self.textField.text || !self.textField.text.length) {
        [self.view makeToast:@"请输入群名称"];
        return;
    }
    [HNSH_ChatNetServer requestRongCloudGroupRenameWithGroupId:self.targetId name:self.textField.text success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [kSTWindow makeToast:@"修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _titleLabel.textColor = [UIColor appBlackLabelColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"修改群聊名称";
    }
    return _titleLabel;
}

- (UILabel *)desLabel{
    if (!_desLabel) {
        _desLabel = [UILabel new];
        _desLabel.font = [UIFont systemFontOfSize:16];
        _desLabel.textColor = [UIColor appBlackLabelColor];
        _desLabel.textAlignment = NSTextAlignmentCenter;
        _desLabel.text = @"修改群聊名称后，将在群内通知其他成员";
    }
    return _desLabel;
}

- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.layer.cornerRadius = 8;
        _imgView.layer.masksToBounds = YES;
        _imgView.image = [UIImage imageNamed:@"chat_icon_myqun"];
    }
    return _imgView;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [UITextField new];
        _textField.placeholder = @"请输入群名称";
        _textField.font = [UIFont systemFontOfSize:16];
        _textField.text = self.name;
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIButton *pwdClean = [_textField valueForKey:@"_clearButton"]; //key是固定的
        [pwdClean setImage:[UIImage imageNamed:@"base_field_clear"] forState:UIControlStateNormal];
        [pwdClean setImage:[UIImage imageNamed:@"base_field_clear"] forState:UIControlStateHighlighted];
    }
    return _textField;
}

- (UIView *)segView{
    if (!_segView) {
        _segView = [UIView new];
        _segView.backgroundColor = [UIColor colorWithHexString:@"#F2F3F6"];
    }
    return _segView;
}

- (UIButton *)doneBtn{
    if (!_doneBtn) {
        _doneBtn = [UIButton new];
        [_doneBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _doneBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _doneBtn.layer.cornerRadius = 8;
        _doneBtn.layer.masksToBounds = YES;
        _doneBtn.backgroundColor = [UIColor appGreenColor];
        [_doneBtn addTarget:self action:@selector(doneBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}

@end
